package hdfs;

import org.apache.commons.io.IOUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;

import org.junit.Test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

public class HdfsDemo {
    private String IP = "hdfs://node01:8020/";
    /**
     * 获取文件系统
     * 有四种方式
     */
    @Test
    public void getFileSystem() throws IOException, URISyntaxException {
        /*方式一：
        Configuration configuration = new Configuration();
        configuration.set("fs.defaultFS", IP);
        FileSystem fileSystem = FileSystem.get(configuration);*/

        /*方式二：
        FileSystem fileSystem = FileSystem.get(new URI(IP),
                new Configuration());*/

        /*方式三：
        Configuration configuration = new Configuration();
        configuration.set("fs.defaultFS", IP);
        FileSystem fileSystem = FileSystem.newInstance(configuration);*/

        /*
        方式四：
         */
        FileSystem fileSystem = FileSystem.newInstance(new URI(IP) ,new Configuration());
        System.out.println(fileSystem.toString());
    }

    /**
     * 遍历指定路径
     */
    @Test
    public void listFile() throws URISyntaxException, IOException {
        FileSystem fileSystem = FileSystem.get(new URI(IP), new Configuration());

        //获取RemoteIterator 得到所有的文件或者文件夹，第一个参数指定遍历的路径，第二个参数表示是否要递归遍历
        RemoteIterator<LocatedFileStatus> locatedFileStatusRemoteIterator = fileSystem.listFiles(new Path("/"), false);
        while (locatedFileStatusRemoteIterator.hasNext()){
            LocatedFileStatus next = locatedFileStatusRemoteIterator.next();
            System.out.println(next.getPath().toString());
        }
        fileSystem.close();
    }

    /**
     * 创建文件夹
     */
    @Test
    public void mkDir()throws URISyntaxException, IOException{
        FileSystem fileSystem = FileSystem.get(new URI(IP), new Configuration());

        Boolean flag = fileSystem.mkdirs(new Path("/test/testdir"));

        fileSystem.close();
    }

    /**
     * 下载文件
     */
    @Test
    public void downloadFile()throws URISyntaxException, IOException{
        FileSystem fileSystem = FileSystem.get(new URI(IP), new Configuration());

        FSDataInputStream inputStream = fileSystem.open(new Path("/a.txt"));
        FileOutputStream outputStream = new FileOutputStream(new File("C:\\Users\\xurui\\Desktop\\123\\b.txt"));

        IOUtils.copy(inputStream, outputStream);

        fileSystem.close();
    }

    /**
     * 上传文件
     */
    @Test
    public void upFile()throws URISyntaxException, IOException{
        FileSystem fileSystem = FileSystem.get(new URI(IP), new Configuration());

        fileSystem.copyFromLocalFile(new Path("file:///C:\\Users\\xurui\\Desktop\\123\\b.txt"), new Path("/c.txt"));

        fileSystem.close();
    }
}
